{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f'(x) = 3*x**2 + 4*x + 1\n"
     ]
    }
   ],
   "source": [
    "# 一阶导数\n",
    "\n",
    "import sympy as sp\n",
    "\n",
    "# 定义变量\n",
    "x = sp.symbols('x')\n",
    "\n",
    "# 定义表达式\n",
    "f = x**3 + 2*x**2 + x\n",
    "\n",
    "# 对 x 求导\n",
    "first_derivative = sp.diff(f, x)\n",
    "print(f\"f'(x) = {first_derivative}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "73a1a751",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f''(x) = 2*(3*x + 2)\n"
     ]
    }
   ],
   "source": [
    "# 二阶导数\n",
    "# 求二阶导数\n",
    "second_derivative = sp.diff(f, x, 2)\n",
    "print(f\"f''(x) = {second_derivative}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "10497a9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "∂f/∂x = 2*x*y + y**2*cos(x)\n",
      "∂f/∂y = x**2 + 2*y*sin(x)\n"
     ]
    }
   ],
   "source": [
    "# 多变量求导\n",
    "# 定义两个变量\n",
    "x, y = sp.symbols('x y')\n",
    "\n",
    "# 多变量表达式\n",
    "f = x**2 * y + sp.sin(x) * y**2\n",
    "\n",
    "# 对 x 求偏导\n",
    "partial_x = sp.diff(f, x)\n",
    "print(f\"∂f/∂x = {partial_x}\")\n",
    "\n",
    "# 对 y 求偏导\n",
    "partial_y = sp.diff(f, y)\n",
    "print(f\"∂f/∂y = {partial_y}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "04b28b37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "∫f(x)dx = x**3/3 + x**2 + x\n"
     ]
    }
   ],
   "source": [
    "# 不定积分\n",
    "# 定义表达式\n",
    "f = x**2 + 2*x + 1\n",
    "\n",
    "# 不定积分\n",
    "indefinite_integral = sp.integrate(f, x)\n",
    "print(f\"∫f(x)dx = {indefinite_integral}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "052c2203",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "∫(0 to 1) f(x) dx = 7/3\n"
     ]
    }
   ],
   "source": [
    "# 定积分 ∫(x^2 + 2x + 1) dx from 0 to 1\n",
    "definite_integral = sp.integrate(f, (x, 0, 1))\n",
    "print(f\"∫(0 to 1) f(x) dx = {definite_integral}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a11ae502",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 多重积分\n",
    "# 定义变量\n",
    "x, y = sp.symbols('x y')\n",
    "\n",
    "# 多变量表达式\n",
    "f = x * y\n",
    "\n",
    "# 二重积分 ∫∫(x * y) dx dy，x: 0 to 1, y: 0 to 2\n",
    "double_integral = sp.integrate(f, (x, 0, 1), (y, 0, 2))\n",
    "print(f\"二重积分结果：{double_integral}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
